package com.qiangesoft.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.im.auth.UserUtil;
import com.qiangesoft.im.entity.ImFriend;
import com.qiangesoft.im.exception.ServiceException;
import com.qiangesoft.im.mapper.ImFriendMapper;
import com.qiangesoft.im.pojo.dto.ImFriendDTO;
import com.qiangesoft.im.pojo.vo.ImFriendVO;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.IImFriendService;
import com.qiangesoft.im.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 好友 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@RequiredArgsConstructor
@Service
public class ImFriendServiceImpl extends ServiceImpl<ImFriendMapper, ImFriend> implements IImFriendService {

    private final ISysUserService sysUserService;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void addFriend(ImFriendDTO friendDTO) {
        Long userId = UserUtil.getUserId();
        Long friendUserId = friendDTO.getFriendUserId();
        if (userId.equals(friendUserId)) {
            throw new ServiceException("不能添加自己！");
        }

        LambdaQueryWrapper<ImFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImFriend::getUserId, userId)
                .eq(ImFriend::getFriendUserId, friendUserId)
                .eq(ImFriend::getDelFlag, false);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("已添加过好友！");
        }

        ImFriend friend = new ImFriend();
        friend.setUserId(userId);
        friend.setFriendUserId(friendDTO.getFriendUserId());
        friend.setRemark(friendDTO.getRemark());
        friend.setDelFlag(false);
        baseMapper.insert(friend);
    }

    @Override
    public void removeFriend(Long id) {
        LambdaUpdateWrapper<ImFriend> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImFriend::getId, id)
                .set(ImFriend::getDelFlag, true);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void updateFriend(Long id, ImFriendDTO friendDTO) {
        LambdaUpdateWrapper<ImFriend> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImFriend::getId, id)
                .set(ImFriend::getRemark, friendDTO.getRemark());
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public List<ImFriendVO> listFriend(String keyword) {
        LambdaQueryWrapper<ImFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(keyword), ImFriend::getRemark, keyword)
                .eq(ImFriend::getDelFlag, false);
        List<ImFriend> friendList = baseMapper.selectList(queryWrapper);
        List<ImFriendVO> friendVOList = new ArrayList<>();
        if (CollectionUtils.isEmpty(friendList)) {
            return friendVOList;
        }

        List<Long> userIdList = friendList.stream().map(ImFriend::getFriendUserId).collect(Collectors.toList());
        Map<Long, SysUserVo> sysUserVoMap = sysUserService.listByIds(userIdList)
                .stream().map(SysUserVo::fromEntity)
                .collect(Collectors.toMap(SysUserVo::getId, sysUserVo -> sysUserVo));
        for (ImFriend friend : friendList) {
            ImFriendVO friendVO = new ImFriendVO();
            friendVO.setId(friend.getId());
            friendVO.setFriendUser(sysUserVoMap.get(friend.getFriendUserId()));
            friendVO.setRemark(friend.getRemark());
            friendVOList.add(friendVO);
        }
        return friendVOList;
    }

    @Override
    public ImFriendVO getFriendInfo(Long id) {
        LambdaQueryWrapper<ImFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImFriend::getId, id)
                .eq(ImFriend::getDelFlag, false);
        ImFriend friend = baseMapper.selectOne(queryWrapper);
        if (friend == null) {
            throw new ServiceException("未添加为好友！");
        }

        SysUserVo sysUserVo = SysUserVo.fromEntity(sysUserService.getById(friend.getFriendUserId()));
        ImFriendVO friendVO = new ImFriendVO();
        friendVO.setId(friend.getId());
        friendVO.setFriendUser(sysUserVo);
        friendVO.setRemark(friend.getRemark());
        return friendVO;
    }
}
